import { createRouter, createWebHistory } from 'vue-router'

// 使用具名导入的懒加载（更清晰）
const routes = [

    {
        path: '/',
        name: 'Home',
        component: () => import('../views/shop/Home.vue'),
        meta: { title: '首页' }
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/auth/Login.vue'),
        meta: { title: '登录' }
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('../views/auth/Register.vue'),
        meta: { title: '注册' }
    },
    {
        path: '/order',
        name: 'Order',
        component: () => import('../views/order/Order.vue'),
        meta: { title: '订单' }
    },
    {
        path: '/payment',
        name: 'Payment',
        component: () => import('../views/order/Payment.vue'),
        meta: { title: '支付' }
    },
    {
        path: '/shop/:id',
        name: 'ShopDetail',
        component: () => import('../views/shop/ShopDetailView.vue') // ✅ 正确路径
    },
    {
        path: '/flashsale',
        name: 'FlashSale',
        component: () => import('../views/shop/FlashSalePage.vue')
    },
    {
        path: '/category/food',
        name: 'FoodCategory',
        component: () => import('../views/shop/FoodCategoryPage.vue')
    },
    {
        path: '/discover',
        name: 'Discover',
        component: () => import('../views/shop/DiscoverPage.vue')
    },
    {
        path: '/order-list',
        name: 'OrderList',
        component: () => import('../views/order/OrderList.vue'),
        meta: { title: '订单列表' }
    },
    {
        path: '/mypage',
        name: 'MyPage',
        component: () => import('../views/auth/MyPage.vue'),
        meta: { title: '我的' }
    }

]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
    scrollBehavior(to, from, savedPosition) {
        return savedPosition || { top: 0 }
    }
})

// 设置页面标题
router.beforeEach((to, from, next) => {
    document.title = to.meta.title || '默认标题'
    next()
})

export default router